﻿using BuildPlanner.Classes;
using BuildPlannerLib;
using BuildPlannerLib.Enums;
using BuildPlannerLib.PlayerActions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace BuildPlanner
{
    public class DataContextMain : DataContextBase
    {
        public Build Build { get; set; }

        public List<string> MapSelectOptions { get; set; }
        public List<string> ExpansionSelectOptions { get; set; }

        public ObservableCollection<PlayerAction> ActionList { get; set; }

        private string mapSelectValue;
        public string MapSelectValue
        {
            get { return mapSelectValue; }
            set
            {
                if (mapSelectValue != value)
                {
                    mapSelectValue = value;
                    NotifyPropertyChanged("MapSelectValue");
                }
            }
        }

        private Race race;
        public Race Race
        {
            get { return race; }
            set
            {
                if (race != value)
                {
                    race = value;
                    NotifyPropertyChanged("Race");
                }
            }
        }

        private string expansionSelectValue;
        public string ExpansionSelectValue
        {
            get { return expansionSelectValue; }
            set
            {
                if (expansionSelectValue != value)
                {
                    expansionSelectValue = value;
                    NotifyPropertyChanged("ExpansionSelectValue");
                }
            }
        }

        private string currentTime;
        public string CurrentTime
        {
            get { return currentTime; }
            set
            {
                if (currentTime != value)
                {
                    currentTime = value;
                    NotifyPropertyChanged("CurrentTime");
                }
            }
        }

        public int Minerals
        {
            get { return Build.Player.Minerals.Amount; }
        }

        public int Gas
        {
            get { return Build.Player.Gas.Amount; }
        }

        public int Supply
        {
            get { return Build.Player.Supply.Amount; }
        }

        public int SupplyMaximum
        {
            get { return Build.Player.SupplyMaximum.Amount; }
        }

        private string status = "Status updates go here.";
        public string Status
        {
            get { return status; }
            set
            {
                if (status != value)
                {
                    status = value;
                    NotifyPropertyChanged("Status");
                }
            }
        }

        public RelayCommand SubmitTimeCommand { get; private set; }

        public DataContextMain(Build build, string mapSelectValue)
        {
            Build = build;

            MapSelectOptions = new List<string>() { "Test" };
            MapSelectValue = mapSelectValue;

            ExpansionSelectOptions = new List<string>() { "Wings of Liberty", "Heart of the Swarm", "HotS Public Test" };
            switch (Build.Expansion)
            {
                case Expansion.WingsOfLiberty:
                    ExpansionSelectValue = "Wings of Liberty";
                    break;
                case Expansion.HeartOfTheSwarm:
                    ExpansionSelectValue = "Heart of the Swarm";
                    break;
                case Expansion.HotSPublicTest:
                    ExpansionSelectValue = "HotS Public Test";
                    break;
                default:
                    ExpansionSelectValue = "";
                    break;
            }

            ActionList = Build.ActionList;

            SubmitTimeCommand = new RelayCommand(SubmitTime, SubmitTimeCanExecute);

            Race = Build.Race;

            UpdateFromBuild();
        }

        public void UpdateFromBuild()
        {
            CurrentTime = Build.CurrentTime.ToString(@"h\:mm\:ss\.ff");
            NotifyPropertyChanged("Minerals");
            NotifyPropertyChanged("Gas");
            NotifyPropertyChanged("Supply");
            NotifyPropertyChanged("SupplyMaximum");
        }

        public bool SubmitTimeCanExecute(object parameter)
        {
            if (CurrentTime != (string)parameter)
                return false;

            if (CurrentTime == Build.CurrentTime.ToString(@"h\:mm\:ss\.ff"))
                return false;

            return true;
        }

        public void SubmitTime(object parameter)
        {
            TimeSpan time = TimeSpan.Parse(CurrentTime);
            Build.SetTime(TimeConvert.ToInt(time));

            UpdateFromBuild();
        }
    }
}
